package com.jiang.myfm.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jiang.myfm.R;
import com.jiang.myfm.adapters.Find2adapter;
import com.jiang.myfm.entities.Chapter;
import com.jiang.myfm.entities.Chapteritem;
import com.jiang.myfm.interfaces.Httpservice;
import com.jiang.myfm.newview.GredView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/12/2.
 */
public class Findfragment_2 extends Fragment implements Callback<Chapter> {
    private ImageView iv;
    private ViewPager vp;
    private GredView gv1, gv2, gv3, gv4, gv5;
    private List<Chapteritem> list = new ArrayList<>();
    private List<Chapteritem> list1, list2, list3, list4, list5;
    private Find2adapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_classify_list, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        gv1 = (GredView) view.findViewById(R.id.gv1);
        gv2 = (GredView) view.findViewById(R.id.gv2);
        gv3 = (GredView) view.findViewById(R.id.gv3);
        gv4 = (GredView) view.findViewById(R.id.gv4);
        gv5 = (GredView) view.findViewById(R.id.gv5);
        iv = (ImageView) view.findViewById(R.id.iv);
        vp = (ViewPager) view.findViewById(R.id.vp);
        list1 = new ArrayList<>();
        list2 = new ArrayList<>();
        list3 = new ArrayList<>();
        list4 = new ArrayList<>();
        list5 = new ArrayList<>();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
                .baseUrl(" http://mobile.ximalaya.com/").build();
        Httpservice service = retrofit.create(Httpservice.class);
        service.getSevice("and-a1", "android", 13, 2).enqueue(this);

    }

    @Override
    public void onResponse(Call<Chapter> call, Response<Chapter> response) {
        list.clear();
        list.addAll(response.body().getList());
        Picasso.with(getActivity()).load(list.get(0).getCoverPath()).into(iv);
        for (int i = 1; i < 7; i++) {
            list1.add(list.get(i));
        }
        for (int i = 7; i < 13; i++) {
            list2.add(list.get(i));
        }
        for (int i = 13; i < 19; i++) {
            list3.add(list.get(i));
        }
        for (int i = 19; i < 25; i++) {
            list4.add(list.get(i));
        }
        for (int i = 25; i < list.size(); i++) {
            list5.add(list.get(i));
        }
        adapter = new Find2adapter(list1, getActivity());
        gv1.setAdapter(adapter);
        adapter = new Find2adapter(list2, getActivity());
        gv2.setAdapter(adapter);
        adapter = new Find2adapter(list3, getActivity());
        gv3.setAdapter(adapter);
        adapter = new Find2adapter(list4, getActivity());
        gv4.setAdapter(adapter);
        adapter = new Find2adapter(list5, getActivity());
        gv5.setAdapter(adapter);
    }

    @Override
    public void onFailure(Call<Chapter> call, Throwable t) {

    }
}
